package com.zhifei.common.utils;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class DownloadTemplateUtil {

    public static void download(HttpServletResponse response, String templateName) throws IOException {
        Workbook workbook = null;
        try {
            InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("temp/" + templateName + ".xlsx");
            assert fis != null;
            workbook = new XSSFWorkbook(fis);
            response.setContentType("application/octet-stream");
            templateName = new String(templateName.getBytes(), "utf-8");
            String fileName = templateName;
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(),"iso-8859-1")+ ".xlsx");
            ServletOutputStream out = null;
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            ((XSSFWorkbook) workbook).close();
            //关闭文件输出流
        }
    }

    public static void download(HttpServletResponse response, String filename, Workbook workbook) {
        ServletOutputStream out = null;
        try {
            if (workbook == null) {
                return;
            }
            response.setContentType("application/binary;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + new String
                    (filename.getBytes("UTF-8"), "iso-8859-1") + "\"");
            out = response.getOutputStream();
            workbook.write(out);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件输出流
            try {
                assert out != null;
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static final String CONTENT_TYPE = "application/x-msdownload";

    /**
     * 下载文件
     *
     * @param response HttpServletResponse
     * @param filename 下载时候的文件名称
     * @param file     要下载的文件对象
     * @return true 下载成功    false 下载失败
     */
    public static boolean downLoadFile(HttpServletResponse response, String filename, File file) {
        boolean result = true;
        FileInputStream input = null;
        OutputStream out = null;
        try {
            //设置response的编码方式
            response.setContentType(CONTENT_TYPE);
            //写明要下载的文件的大小
            response.setContentLength((int) file.length());
            //设置附加文件名
            response.setHeader("Content-Disposition", "attachment;filename=\"" + new String
                    (filename.getBytes("UTF-8"), "iso-8859-1") + "\"");
            //读出文件到i/o流
            input = new FileInputStream(file);
            //从response对象中得到输出流,准备下载
            out = response.getOutputStream();
            // 判断输入或输出是否准备好
            if (out != null) {
                int temp = 0;
                try {
                    // 开始拷贝
                    while ((temp = input.read()) != -1) {
                        // 边读边写
                        out.write(temp);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    result = false;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            result = false;
        } catch (IOException e) {
            result = false;
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    result = false;
                }
            }
            if (out != null) {
                try {
                    out.flush();
                    //关闭输出流
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    result = false;
                }
            }
        }
        return result;
    }
}